<template>
    <div class="app-container" ref="content">
        <div style="text-align: center;padding: 10px">个人中心</div>
        <el-row :gutter="10">
            <el-form :model="adminInfo" ref="adminInfo"  label-width="100px">
                <el-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
                    <div style="text-align: center">
                        <img v-if="adminInfo.avatar" :src="adminInfo.avatar" class="avatar">
                        <i v-else class="el-icon-picture-outline avatar-uploader-icon"></i>
                        <div style="line-height: 44px">*头像*</div>
                    </div>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="用户名" prop="name" :rules="nameEdit?{ required: true, message: '用户名不能为空', trigger: 'blur' }:{}">
                            <span v-if="!nameEdit">
                                {{adminInfo.name}}
                                <i class="el-icon-edit pointer" style="margin-left: 10px" @click="editInfo('name')"></i>
                            </span>
                            <div v-else>
                                <el-input  v-model="adminInfo.name" style="width: calc(100% - 40px)" clearable></el-input>
                                <i class="el-icon-check pointer" style="margin-left: 10px" @click="updateInfo('name')"></i>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="手机号" prop="mobile">
                            {{adminInfo.mobile}}
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="昵称" prop="nickName" :rules="nickNameEdit?{ required: true, message: '昵称不能为空', trigger: 'blur' }:{}">
                            <span v-if="!nickNameEdit">
                                {{adminInfo.nickName}}
                                <i class="el-icon-edit pointer" style="margin-left: 10px" @click="editInfo('nickName')"></i>
                            </span>
                            <div v-else>
                                <el-input  v-model="adminInfo.nickName" style="width: calc(100% - 40px)" clearable></el-input>
                                <i class="el-icon-check pointer" style="margin-left: 10px" @click="updateInfo('nickName')"></i>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="所属人" prop="person" :rules="personEdit?{ required: true, message: '所属人不能为空', trigger: 'blur' }:{}">
                             <span v-if="!personEdit">
                                {{adminInfo.person}}
                                <i class="el-icon-edit pointer" style="margin-left: 10px" @click="editInfo('person')"></i>
                            </span>
                            <div v-else>
                                <el-input  v-model="adminInfo.person" style="width: calc(100% - 40px)" clearable></el-input>
                                <i class="el-icon-check pointer" style="margin-left: 10px" @click="updateInfo('person')"></i>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="性别" prop="sex">
                            {{adminInfo.sex | formDateSex}}
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8" >
                        <el-form-item label="邮箱" prop="email" :rules="emailEdit?{ required: true, message: '邮箱不能为空', trigger: 'blur' }:{}">
                            <span v-if="!emailEdit">
                                {{adminInfo.email}}
                                <i class="el-icon-edit pointer" style="margin-left: 10px" @click="editInfo('email')"></i>
                            </span>
                            <div v-else>
                                <el-input  v-model="adminInfo.email" style="width: calc(100% - 40px)" clearable></el-input>
                                <i class="el-icon-check pointer" style="margin-left: 10px" @click="updateInfo('email')"></i>
                            </div>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="所属角色" prop="role">
                            {{adminInfo.role}}
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="帐号状态" prop="status">
                            {{adminInfo.status?"启用":"禁用"}}
                        </el-form-item>
                    </el-col>
                </el-col>
            </el-form>
        </el-row>
    </div>
</template>

<script>
import {getInAdminInfo, updateInAdmin} from "@/api/admin";

export default {
    name: "adminInfo",
    data(){
        return{
            adminInfo:{},
            nameEdit:false,
            nickNameEdit:false,
            personEdit:false,
            emailEdit:false
        }
    },
    created() {
        this.$nextTick(() => {
            this.getInAdminInfo()
        });
    },
    methods:{
        getInAdminInfo(val){
            getInAdminInfo().then(res=>{
                this.adminInfo = res.data
            }).catch(res=>{

            })
        },
        editInfo(val){
            let name = val+"Edit";
            this[name] = true
        },
        updateInfo(val){
            //更新信息
            this.$refs['adminInfo'].validate(valid => {
                if (valid){
                    updateInAdmin(this.adminInfo).then(res=>{
                        this.$notify.success({
                            title: '成功',
                            message: "更新个人信息成功"
                        })
                        let name = val+"Edit";
                        this[name] = false
                    }).catch(res=>{
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    console.log("表单验证不通过")
                }
            })
        }
    }
}
</script>

<style>
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 149px;
    height: 149px;
    line-height: 149px;
    text-align: center;
}
.avatar {
    width: 149px;
    height: 149px;
    display: block;
    margin: 0 auto; /*水平居中*/
}
</style>